<?php defined('SYSPATH') or die('No direct script access.');
class OrgRegistration_Controller extends OrgController {

    public $REGISTRATION_LIST_UUID = 'org/orgSeminar/orgRegistrationList';
    public $CANCELLED_REGISTRATION_LIST_UUID = 'org/orgSeminar/orgCancelledRegistrationList';
    public $UNCONFIRMED_REGISTRATION_LIST_UUID = 'org/orgSeminar/orgUnconfirmedRegistrationList';
    
    public function __construct() {
        parent::__construct();
        $this->activeMenuItem = OrgMenu::$SEMINARS;
    }

    public function listing() {
        $seminarId = $this->input->get('seminar_id');
        if (! valid::isId($seminarId)) {
            $this->msg->error('error.invalidId');
            $this->redirect('/org/orgSeminar/all');
        }
        
        $seminar = $this->dao->getLocalizedEntityById('seminar', $seminarId, $this->getL10nLanguageCode());
        if (! $seminar) {
            $this->msg->error('error.invalidId');
            $this->redirect('/org/orgSeminar/all');
        }
        $this->viewData = $this->getSeminarService()->getOrgAccountingData($seminarId, $this->getAuthOrg('id'));
        $this->viewData['seminar'] = $seminar;
        $this->renderMySubview('orgRegistrationList');
    }

    public function display() {
        $registrationId = $this->input->get('id');
        
        if (! valid::isId($registrationId)) {
            $this->redirectToList('error.invalidId');
        }

        $registration = $this->getSeminarService()->loadRegistrationDisplayData($registrationId);
        if (! $registration) {
            $this->redirect('/org/orgSeminar/all');
        }
        $this->viewData['registration'] = $registration;
        
        $this->viewData['paymentAcceptance'] = $this->dao->getPaymentAcceptance($registration['seminar_id'], $this->getAuthOrg('id'));
        
        $this->renderMySubview('orgRegistrationDisplay');
    }
    
    public function confirm() {
        $id = $this->input->get('id');
        
        if (! valid::isId($id)) {
            $this->msg->error('error.invalidId');
            $this->redirect('/org/orgSeminar/all');
        }

        $registration = $this->dao->getById('registration', $id);
        if (! $registration) {
            $this->msg->error('error.recordNotFound');
            $this->redirect('/org/orgSeminar/all');
        }
        
        $paymentAcceptance = $this->dao->getPaymentAcceptance($registration['seminar_id'], $this->getAuthOrg('id'));
        if ($paymentAcceptance && ! $paymentAcceptance['confirmed_at']) {
        	$this->getSeminarService()->confirmRegistration($registration);
        }

        $this->redirect('/org/orgSeminar/registrationList?id=' . $registration['seminar_id']);
    }
    
    public function orgNotesForm() {
        $id = $this->input->get('id');

        if (! valid::isId($id)) {
            $this->msg->error('error.invalidId');
            $this->redirect('/org/orgSeminar/all');
        }

        $registration = $this->dao->getById('registration', $id);
        if (! $registration) {
            $this->msg->error('error.recordNotFound');
            $this->redirect('/org/orgSeminar/all');
        }
        $this->viewData['registration'] = $registration;
        
        $seminar = $this->dao->getLocalizedEntityById('seminar', $registration['seminar_id'], $this->getL10nLanguageCode());
        $this->viewData['seminar'] = $seminar;
        
        $person = $this->dao->fetchPersonById($registration['person_id']);
        $this->viewData['person'] = $person;
        
        $this->renderMySubview('addOrgNotes');
    }
    
    public function addOrgNotes() {
        $formData = $this->postToDbValues();
        $registration = $this->dao->getById('registration', $formData['id']);
        $registration['org_notes'] = $formData['org_notes'];
        $this->dao->update('registration', $registration);
        $this->msg->info('registration-orgNotes-saveSuccess', $registration['id']);
        $this->redirect('/org/orgSeminar/registrationList?id=' . $registration['seminar_id']);
    }
    
    public function saveOrgNotes() {
        // TODO
    }
    
    public function cancel() {
    	$registrationId = $this->input->post('id');
    	$reason = $this->input->post('cancellation_reason');

        if (! valid::isId($registrationId)) {
        	$this->redirectToList('error.invalidId');
        }
        
        $registration = $this->dao->getById('registration', $id);
        if (! $registration) {
        	$this->msg->error('error.recordNotFound');
        	$this->redirect('/org/orgSeminar/all');
        }
        
        $paymentAcceptance = $this->dao->getPaymentAcceptance($registration['seminar_id'], $this->getAuthOrg('id'));
        if ($paymentAcceptance && ! $paymentAcceptance['confirmed_at']) {
        	$registration = $this->getSeminarService()->cancelRegistration($registrationId, $reason);
        }
        
		$this->redirect('/org/orgSeminar/registrationList?id=' . $registration['seminar_id']);
    }
    
    public function edit() {
    	$id = $this->input->get('id');
        
        if (! valid::isId($id)) {
            $this->redirectToList('error.invalidId');
        }

        $registration = $this->getSeminarService()->loadRegistrationDisplayData($id);
        if (! $registration || $registration['payment_organization_id'] != $this->getAuthOrg('id')) {
            $this->redirect('/org/orgSeminar/all');
        }

    	$this->editFormData($registration);
    	$this->renderMySubview('orgRegistrationEdit');
    }
    
    private function editFormData($registration) {
    	$viewData = $this->getSeminarService()->loadRegistrationEditFormData($registration);
    	$this->mergeViewData($viewData);
    }
    
    public function save() {
    	$formData = $this->postToDbValues();
        $id = $formData['id'];

        // Basic validation
        $validator = new EntityValidator($formData, 'registration');
        $validator->code('batch_code');
        $validator->code('category_code');
        $validator->integerOrNull('price_paid');
        $validator->booleanOrNull('social_discount_applied');
        
        if ($this->msg->hasErrors()) {
        	$registration = $this->getSeminarService()->loadRegistrationDisplayData($id);
        	$mergedRegistration = array_merge($registration, $formData);
            $this->editFormData($mergedRegistration);
            $this->renderMySubview('orgRegistrationEdit');
            return;
        }
        
        // Database validation
        $registration = $this->dao->getById('registration', $id);
        if (! $registration || $registration['payment_organization_id'] != $this->getAuthOrg('id')) {
        	$this->msg->error('error.recordNotFound');
        	$this->redirect('/org/orgSeminar/all');
        }
        
        $paymentAcceptance = $this->dao->getPaymentAcceptance($registration['seminar_id'], $this->getAuthOrg('id'));
        if ($paymentAcceptance && ! $paymentAcceptance['confirmed_at']) {
        	$this->getSeminarService()->saveEditedRegistration($registration, $formData, UserRole::$org);
        }
        
        $this->redirect('/org/orgRegistration/display?id=' . $id);
    }
}
